// pages/maitian/card/card.js

var WxParse = require('../../../wxParse/wxParse.js');
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgurl: app.globalData.imgurl,
    showmore:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    this.testMine(e)
  },
  // 是否我的名片
  testMine(e) {
    this.setData(e)
    if (e.mine) {
      var name = '我的名片'
      this.getmycard()

      wx.setNavigationBarTitle({
        title: name,
      })
    } else {
      this.getCardDetail(e)
    }
  },
  // 名片详情
  getCardDetail: function (e) {
    var that = this
    wx.request({
      url: app.globalData.url + 'Card/detail?buid=' + e.id + '&wxopenid=' + wx.getStorageSync('openId'),
      success: res => {
        var data = res.data
        var carddata = res.data.data
        var art = carddata.content;
        WxParse.wxParse('art', 'html', art, that, 5);
        carddata.service = carddata.service.split(',')
        wx.setNavigationBarTitle({
          title: carddata.username+'的名片',
        })
        that.setData({
          carddata: data.data,
          follow: data.follow,
          zan: data.zan
        })
      }
    })
  },
  // 关注
  guanzhu: function (e) {
    var that = this
    var o = e.currentTarget.dataset
    wx.request({
      url: app.globalData.url + 'Card/shoucanguser?buid=' + o.id + "&status=" + ((o.follow + 1) % 2) + '&wxopenid=' + wx.getStorageSync('openId'),
      success: res => {
        console.log(res)
        that.getCardDetail(o)
      }
    })
  },
  showmore:function(){
    this.setData({
      showmore:1
    })
  },
  dashang: function () {
    var that = this
    this.setData({
      iswxzf: true
    })
  },
  getMoney: function (e) {
    this.setData({
      money: e.detail.value
    })
  },
  pay: function () {
    var that = this
    wx.request({
      url: app.globalData.url + 'Card/dashangpay',
      data: {
        wxopenid: wx.getStorageSync('openId'),
        buid: that.data.carddata.id,
        money: that.data.money
      },
      success: function (res) {
        console.log(res)
        wx.request({
          url: app.globalData.url + 'Pay',
          data: {
            wxopenid: wx.getStorageSync('openId'),
            order_id: res.data.order_id
          },
          success: function (res) {
            console.log(res)
            var res = res.data
            wx.requestPayment({
              timeStamp: res.timeStamp,
              nonceStr: res.nonceStr,
              package: res.package,
              signType: res.signType,
              paySign: res.paySign,
              success: function (res) {
                that.setData({
                  success: true,
                  baoming: 0
                })
                //that.geDetail({ id: that.data.ac_info.id })  // 报名成功刷新页面

              },
              fail: () => {
                wx.showModal({
                  title: '提示',
                  content: '支付取消',
                  showCancel: false,
                  confirmColor: '#ffbf00',
                })
              },
              complete: () => {
                that.setData({
                  iswxzf: false
                })
              }
            })
          },
          fail: function (res) { },
          complete: function (res) { },
        })
      }
    })

  },
  close:function(){
    that.setData({
      iswxzf: false,
      success:false
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  // 我的名片
  getmycard: function () {
    var that = this
    wx.request({
      url: app.globalData.url + 'Card/myindex?wxopenid=' + wx.getStorageSync('openId'),
      success: function (res) {
        var data = res.data
        var carddata = data.data
        console.log(carddata)
        if (data.data == null) {
          wx.navigateTo({
            url: '/pages/mycards/addcard/addcard',
          })
          return
        }
        var art = carddata.content;
        WxParse.wxParse('art', 'html', art, that, 5);
        carddata.service = carddata.service.split(',')
        that.setData({
          carddata: carddata,
          userhuodong: data.userhuodong,
          userorganize: data.userorganize,
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})